﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using Microsoft.SharePoint.Utilities;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;


using System.Collections.Generic;
using System.Linq;

namespace WebpartSample.VisualWPSample
{
    public partial class VisualWPSampleUserControl : UserControl
    {
        public static string listTitleStr;
        protected void Page_Load(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb we = site.OpenWeb(SPContext.Current.Web.ID))
                {
                    StringBuilder x = new StringBuilder();
                    x.Append("[");
                    foreach (SPListItem item in we.Lists["ListTimerJob"].Items)
                    {
                        x.Append(string.Format("\"{0}\", ",item["Title"].ToString()));
                    }
                    listTitleStr = x.ToString().Substring(0,x.ToString().Length-2)+"]";
                }
            }
            if (IsPostBack)
            {
                int i;
                int.TryParse(TextBox1.Text, out i);
                i = i + 1;
                TextBox1.Text = i + "";
            }
            else
                TextBox1.Text = "5";
            lblIncVal.Text = TextBox1.Text;
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            txtTextBox.Text= Request.Url.ToString().Split('?').FirstOrDefault(); 
            //BindSpGrid();
            //StringDictionary headers = new StringDictionary();

            //headers.Add("from", "livigl@gmail.com");
            //headers.Add("to", "livingston@triadinfotech.com");
            //headers.Add("subject", "Welcome to the SharePoint group: ABC site: ");
            //headers.Add("content-type", "text/html"); //This is the default type

            //System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
            //strMessage.Append("<br><br><b>Login Instructions: </b><br>");
            //strMessage.Append("<font color='red'><UL><li>If you are a US employee ALWAYS login in using the following login format </font><br>");
            ///*
            // stsadm -o email 
            // -outsmtpserver mail.triadinfotech.com 
            // -replytoaddress livingston@triadinfotech.com 
            // -fromaddress livingston@triadinfotech.com 
            // -codepage 65001 
            // -url http://triad125:22329
            //*/
            //SPSecurity.RunWithElevatedPrivileges(delegate()
            //{
            //    SPUtility.SendEmail(SPContext.Current.Web, headers, strMessage.ToString());
            //});
        }

        protected void txtSendEmail_Click(object sender, EventArgs e)
        {
            StringDictionary headers = new StringDictionary();
            headers.Add("from", "liviggl@gmail.com");
            headers.Add("to", "livingston@triadinfotech.com");
            headers.Add("subject", "testmail");

            System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
            strMessage.Append("<head><style type=\"text/css\">.style1 {font-size: small;       border: 1px solid #000000;" +
                            "background-color: #DEE7FE;}.style2 {border: 1px solid #000000;}</style></head>" +
                            "<p>The Project site <strong> web.Title </strong> has been created. Please <a href='http://www.google.com'>click here</a> to view the project site.</p>");
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPUtility.SendEmail(SPContext.Current.Web, headers, strMessage.ToString());
            });
        }

        private void BindSpGrid()
        {
            Guid gid = SPContext.Current.Site.ID;
            SPWeb wb = new SPSite(gid).OpenWeb();
            SPList ol = wb.Lists.TryGetList("ListTimerJob");



            spgvTest.DataSource = ol.Items.GetDataTable();
            spgvTest.Columns.Add(new SPBoundField { DataField = "Title", HeaderText = "e-Mail" });
            spgvTest.Columns.Add(new SPBoundField { DataField = ol.Items.GetDataTable().Columns[1].ColumnName, HeaderText = "when" });
            spgvTest.DataBind();
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void dotNeteMailSend_Click(object sender, EventArgs e)
        {
            //System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            //message.To.Add("livingston@triadinfotech.com");
            //message.Subject = "This is the Subject line";
            //message.From = new System.Net.Mail.MailAddress("liviggl@gmail.com");
            //message.Body = "<head><style type=\"text/css\">.style1 {font-size: small;       border: 1px solid #000000;" +
            //                "background-color: #DEE7FE;}.style2 {border: 1px solid #000000;}</style></head>" +
            //                "<p>The Project site <strong> web.Title </strong> has been created. Please <a href='http://www.google.com'>click here</a> to view the project site.</p>";
            //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.triadinfotech.com");
            //smtp.Send(message);
            SendMail(txtTo.Text, txtBody.Text);
        }
        private void SendMail(string To, string Body)
        {
            System.Net.Mail.SmtpClient Mailing = new System.Net.Mail.SmtpClient("mail.triadinfotech.com");
            System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();

            Message.From = new System.Net.Mail.MailAddress("liviggl@gmail.com");

            Message.Subject = "Subject";
            Message.SubjectEncoding = Encoding.UTF8;

            Message.IsBodyHtml = true;
            Message.BodyEncoding = Encoding.UTF8;
            Message.Body = Body;

            Message.To.Add(new System.Net.Mail.MailAddress(To));

            //Mailing.UseDefaultCredentials = false;
            //NetworkCredential MyCredential = new NetworkCredential("mail@domain.com", "password");
            //Mailing.Credentials = MyCredential;

            Mailing.Send(Message);
        }
    }
}
